
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>学生管理</title>
    <link rel="icon" href="static/favicon.ico">
    <link rel="stylesheet" href="static/bootstrap/css/bootstrap.min.css" >
    <link rel="stylesheet" href="static/admin/css/index.css" >
    <link rel="stylesheet" href="static/admin/css/main.css" >
    <link rel="stylesheet" href="static/admin/css/html.css" >
    <script src="static/js/vue.min.js"></script>
    <script src="static/js/jquery-3.3.1.min.js" ></script>
    <script src="static/bootstrap/js/bootstrap.bundle.js" ></script>
    <script src="static/admin/js/script.js" ></script>
    <script>
        $(function () {
            let hrefArr = window.location.href.split('/');
            let name = hrefArr[hrefArr.length-1];
            $(".menu a[href='"+name+"']").addClass('active');
        })
    </script>
</head>
<body>
<main id="showmain">
    <div class="main">
        <div class="search">
            <form class="form-inline float-left" >
                <div class="form-group">
                    <input type="text" id="name" name="name"  class="form-control" placeholder="姓名">
                </div>
                <button type="button" onclick="query(1)" class="btn btn-info"><i class="iconfont"></i>搜索</button>
            </form>
            <a class="btn btn-info float-right" href="javascript:add_user();"><i class="iconfont"></i>新增</a>
        </div>
        <div class="table-box">
            <table class="table">
                <thead>
                <tr>
                    <th>姓名</th>
                    <th>职务</th>
                    <th>url</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="tb">
                </tbody>
            </table>
        </div>
        <div class="page">
            <span class="total float-left page-link" id="tiao">共23223条</span>
            <span class="total float-left page-link" id="cpage">第111111页</span>
            <span class="total float-left page-link" id="ye">共23223页</span>
            <ul class="pagination">
                <input type="hidden" id="pageNo">
                <li class="page-item"><a class="page-link" href="javascript:beforPage();">上一页</a></li>
                <!-- <li class="page-item"><a class="page-link" href="avascript:page('+a+');">a</a></li>
         -->
                <li ><input id="page_no" type="text" style="width:40px; height:40px;"  /></li>
                <li class="page-item"><a class="page-link" href="javascript:nextPage();">下一页</a></li>

                <li class="page-item"><a class="page-link" href="javascript:jump();">跳转</a></li>
            </ul>
        </div>
    </div>
</main>

<main id="addmian">
    <div class="main">
        <div class="title-box">
            <h5 id="biaoti">新增员工</h5>
        </div>
        <div class="form-box">
            <div class="row">
                <div class="col-5">
                    <form id="form">
                        <input id="id" name="id" type="hidden"/>
                        <div class="form-group">
                            <label>姓名</label>
                            <input id="ljyname" type="text" name="name" class="form-control">
                            <small class="form-text text-muted"></small>
                        </div>
                        <div class="form-group">
                            <label>url</label>
                            <input id="ljyurl" type="text" name="url" class="form-control">
                            <small class="form-text text-muted"></small>
                        </div>
                        <div class="form-group">
                            <label>职务</label>
                            <select id="dept" name="bid" class="form-control">
                                <option value="1">1</option>
                                <option value="2">2</option>
                            </select>
                            <small class="form-text text-muted"></small>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div class="submit-box">
            <button type="button" onclick="sub()" class="btn btn-primary">确定提交</button>
            <!--window.history.back() back() 方法可加载历史列表中的前一个 URL（如果存在）-->
            <button type="button" onclick="show_user()" class="btn btn-outline-secondary">取消</button>
        </div>
    </div>
</main>

</body>
<script>
    $(function () {
        query(1);//默认显示第一页
        $("#addmian").hide();
    });

    function beforPage() {
        var b =parseInt($("#pageNo").val()) - 1;
        if(b < 1) {b =1 }
        query(b);
    }
    function nextPage() {
        var b =parseInt($("#pageNo").val()) + 1;
        query(b);
    }
    function jump() {
        var pageno=$("#page_no").val();
        //alert(typeof(pageno));
        if(pageno!=null && typeof(pageno)=="string"){
            if(pageno<1){
                query(1);
            } else if(pageno>$("#ye").text()){
                query($("#ye").text());
            }else{
                query(pageno);
            }
        }
        /*if(pageno==""){
            query($("#pageNo").val());
        }*/

    }
    //显示记录
    function query(a) {
        var name = $("#name").val();//获取姓名文本框
        $.ajax({
            type:"post",
            url:"/ssm_1905_lijinyuan/showAll",//controller --->404
            data:{"name":name,"pageNo":a},
            dataType:"json",
            success:function (res) {
                var ht ='';
                $("#tb").empty();
                $.each(res.data,function (i,v) {
                    ht = ht+ '<tr class="id9">'+
                        '<td>'+v.name+'</td>' +
                        '<td>'+v.bname+'</td>'+
                        '<td>'+v.url+'</td>'+
                        '<td><a href="javascript:update_user('+v.id+');">修改</a> | <a class="del" data-id=".id9" href="javascript:deluser('+v.id+');">删除</a></td>' +
                        '</tr>';
                });
                //console.log(ht)
                 $("#tb").append(ht);
                 $("#tiao").text('共'+res.totalCount+'条');
                 $("#ye").text('共'+res.totalPage+'页');
                 $("#pageNo").val(res.currentPage);
                 $("#cpage").text('第'+res.currentPage+'页');
            }
        });
    }
    //隐藏显示记录界面，显示添加界面
    function add_user(id) {
        $("#showmain").hide();
        $("#addmian").show();

        $("#biaoti").text("新增员工");
        $("#ljyname").val("");
        $("#ljyurl").val("");
        $("#id").val("");
        if(id!=""){
            show_dept(id);
        }else{
            show_dept();
        }
    }

    function show_user(){
        $("#showmain").show();
        $("#addmian").hide();
    }
    //添加提交
    function sub() {
        $.ajax({
            type:"post",
            url:"/ssm_1905_lijinyuan/addOne",
            data:$("#form").serialize(),//{'name':name，‘age’:age}
            async:false,
            success:function () {
                //alert("添加成功!");
                window.location.reload();
                /*$("#showmain").show();
                $("#addmian").hide();*/
                //window.location.href="index.html";
            }

        });
    }
    //修改
    function update_user(e){

        var id =e;
        $.ajax({
            type:"post",
            url:"/ssm_1905_lijinyuan/showOneId",
            data:{"id":id},
            dataType:"json",
            ansyc:false,
            success:function (data) {
                var bid ="";
                bid =data.bid;
                add_user(bid);//显示修改界面
                $("#biaoti").text("修改员工");
                if(data != null) {
                    $("#ljyname").val(data.name);
                    $("#ljyurl").val(data.url);
                    $("#id").val(data.id);
                }
            }
        });
    }
    //删除方法
    function deluser(e) {
        var id=e;
        $.ajax({
            type:"post",
            url:"/ssm_1905_lijinyuan/delOneId",
            data:{"id":id},
            dataType:"json",
            ansyc:false,
            success:function (result) {
                alert("删除成功！");
                window.location.reload();
            }
        });
    }
    //显示职务
    function show_dept(bid){

        $.ajax({
            type:"post",
            url:"/ssm_1905_lijinyuan/dept_showAll",
            dataType:"json",
            success:function (result) {
             $("#dept").empty();
              var ht = '';
              $.each(result,function (i,v) {
                  var c ='';
                  if(v.id ==bid ){c ='selected=true'};

                  ht = ht + '<option '+c+' value="'+v.id+'">'+v.bname+'</option>';
              });
              $("#dept").append(ht);
            }
        });
    }

</script>
</html>
